﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElectronicRecords.Models;
using System.Web.Security;

namespace ElectronicRecords.Controllers
{
    public class EquipmentsController : Controller
    {
        //
        // GET: /Equipments/
        private DataManager _dataManager;

        public EquipmentsController(DataManager datamanager)
        {
            _dataManager = datamanager;
        }

        public ActionResult List()
        {
            ViewData["Title"] = "Справочник оборудования";
            ViewData["Equipments"] = _dataManager.Equipments.GetEquipments();
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Equipments e)
        {
            if (e.Name == null || e.Name.Length < 1) ModelState.AddModelError("Name", "Неверное содержимое поля");
            if (e.Description == null || e.Description.Length < 1) ModelState.AddModelError("Description", "Неверное содержимое поля");
            if (ModelState.IsValid)
            {
                
                MembershipUser u = Membership.GetUser(User.Identity.Name);

                e.CreatedBy = (Guid)u.ProviderUserKey;
               
               _dataManager.Equipments.CreateEquipment(e);
                return RedirectToAction("List");
            }
            return View();
        }

        public ActionResult Equipment(int id)
        {
            ViewData.Model = _dataManager.Equipments.GetEquipment(id);

            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(int id)
        {
            ViewData.Model = _dataManager.Equipments.GetEquipment(id);
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, Equipments e)
        {
            if (e.Name == null || e.Name.Length < 1) ModelState.AddModelError("Name", "Неверное содержимое поля");
            if (e.Description == null || e.Description.Length < 1) ModelState.AddModelError("Description", "Неверное содержимое поля");
            if (ModelState.IsValid)
            {
                e.ID_Equipment = id;
                MembershipUser u = Membership.GetUser(User.Identity.Name);

                e.CreatedBy = (Guid)u.ProviderUserKey;

                _dataManager.Equipments.UpdateEquipment(e);
                return RedirectToAction("List");
            }
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Find()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Find(string searchStr)
        {
            if (Request.IsAjaxRequest())
            {
                
                List<Equipments> l = _dataManager.Equipments.FindEquipments(searchStr).ToList<Equipments>();
                string result = "";
                result += "<table id=\"maintable\">";
                result += " <tr><th style=\"width:10%\">Действие</th> <th style=\"width:10%\">ID</th> <th style=\"width:80%\">Название</th> </tr>";
                foreach (Equipments c in l)
                {
                    result += "<tr><td><a href= \"Equipment/" + c.ID_Equipment + "\"><img height=\"24\" width=\"24\" src=\"../../img/sherlock.png\" ></a> <a href= \"Edit/" + c.ID_Equipment + "\"><img height=\"24\" width=\"24\"  src=\"../../img/textedit.png\" ></a></td><td>" + c.ID_Equipment + "</td><td>" + c.Name + "</td></tr>";

                }
                result += "</table>";
                return Content(result);
            }
            return View();
        }

    }
}
